﻿
Imports std
Imports xmlctrl
Imports xhtmlctrl
Imports webctrl
Imports wfileis.data

Public Module preview
    Public Const quickcommentFunction As String = "quickcomment"
    Public Const quickcommentExFunction As String = "quickcommentex"
    Public Const pvClass As String = "pv"

    Private Sub processInfo(ByVal context As httpContext, ByVal page As xhtmlpage, _
                            ByVal root As xmlcontrol, ByVal key As String, ByVal info As info, _
                            ByVal promoteMultimediaContent As Boolean, _
                            ByVal maxlength As Int64)
        assert(Not context Is Nothing, "context is nothing.")
        assert(Not page Is Nothing, "page is nothing.")
        assert(Not root Is Nothing, "root is nothing.")

        If Not info Is Nothing Then
            Dim pv As posterViewer = Nothing
            pv = context.createControl(New posterViewer())
            Dim ava As avatar = Nothing
            ava = context.createControl(New avatar())
            ava.setData(info)
            pv.children().push_front(ava)
            Dim ds As posterViewer.datasource
            ds = New posterViewer.datasource()
            ds.page = page
            ds.text = info.text()
            ds.isIEUserAgent = context.isIEUserAgent()
            ds.maxlength = maxlength
            ds.promoteMultimediaContent = promoteMultimediaContent
            pv.setData(ds)
            pv.class = pvClass
            root.addchild(pv)

#If RETIRED Then
          If Not isemptystring(info.id) Then
                a = context.createControl(New a())
                a.href = user.generateUserViewlink(context, info.id)
                a.class = "id"
                a.text = "-- " + info.id
                root.addchild(a)
            Else
                Dim xmltext As xmltext = Nothing
                xmltext = context.createControl(New xmltext())
                xmltext.text = "-- " + context.string(stringId.INFO_NOID)
                root.addchild(xmltext)
            End If
#End If

            Dim idcontainer As div = Nothing
            idcontainer = context.createControl(New div())
            idcontainer.class = "id"
            root.addchild(idcontainer)

            Dim idshow As idshow = Nothing
            idshow = context.createControl(New idshow())
            idshow.setData(info, "--")
            idcontainer.addchild(idshow)
        Else
            Dim div As div = Nothing
            div = context.createControl(New div())
            div.text = context.string(stringId.VIEW_FAILREADINFO) + " [" + key + "]"
            root.addchild(div)
        End If
    End Sub

    Private Sub processPreview(ByVal context As httpContext, ByVal page As xhtmlpage, _
                               ByVal root As xmlcontrol, ByVal key As String, _
                               ByVal record As record, ByVal infos As vector(Of pointer(Of info)), _
                               ByVal maxlength As Int64)
        assert(Not context Is Nothing, "context is nothing.")
        assert(Not page Is Nothing, "page is nothing.")
        assert(Not root Is Nothing, "root is nothing.")
        assert(Not record Is Nothing, "record is nothing.")
        assert(Not infos Is Nothing AndAlso infos.size() > 0, "infos is nothing, or empty.")

        Dim h4 As h4 = Nothing
        h4 = context.createControl(New h4())
        h4.class = "t"
        root.addchild(h4)

        Dim a As a = Nothing
        a = context.createControl(New a())
        a.href = view.generateViewlink(context, key)
        a.text = key
        h4.addchild(a)

        Dim replyCount As span = Nothing
        replyCount = context.createControl(New span())
        replyCount.class = "rpc"
        replyCount.text = Convert.ToString(record.size() - 1) + context.string(stringId.REPLY_COUNT)
        root.addchild(replyCount)

        processInfo(context, page, root, record(0), +(infos(0)), True, maxlength)
        If infos.size() > 1 Then
            Dim rpl As div = Nothing
            rpl = context.createControl(New div())
            rpl.class = "rpl"
            root.addchild(rpl)

            Dim replyMsg As div = Nothing
            replyMsg = context.createControl(New div())
            replyMsg.text = context.string(stringId.REPLY_MESSAGE)
            replyMsg.class = "rpm"
            rpl.addchild(replyMsg)

            Dim i As Int64
            For i = 1 To infos.size() - 1
                processInfo(context, page, rpl, record(record.size() - infos.size() + i), +(infos(i)), False, maxlength)
            Next
        End If
    End Sub

    Public Sub processPreview(ByVal context As httpContext, _
                              ByVal key As String, ByVal record As record, _
                              ByVal infos As vector(Of pointer(Of info)), _
                              ByVal page As xhtmlpage, ByVal root As xmlcontrol, _
                              Optional ByVal maxlength As Int64 = maxInt64)
        Dim div As div = Nothing
        div = context.createControl(New div())
        div.class = "b"
        page.inlineScript.addscript(quickcommentExFunction, _
                                    "function " + quickcommentExFunction + "(t,e){" + _
                                        ifelse.invoke( _
                                            toLowerCase.invoke(page.inlineScript, _
                                                               eventTarget.invoke(page.inlineScript(), "e") + _
                                                               ".tagName") + _
                                            "!=""a""&&" + _
                                            existfunction.invoke(page, "'" + quickcommentFunction + "'"), _
                                            quickcommentFunction + "(t,e)") + "}")
        Dim jsescapkey As String = Nothing
        copy(jsescapkey, key)
        div.onclick = quickcommentExFunction + "('" + strrplc(jsescapkey, "'", "\'") + "',event)"
        colorfulControl.invoke(page, div)
        root.addchild(div)

        Dim div2 As div = Nothing
        div2 = context.createControl(New div())
        div2.class = "b2"
        div.addchild(div2)

        processPreview(httpServer2.currentContext(), page, div2, key, record, infos, maxlength)
    End Sub

End Module
